package com.hongye.app.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Model;
/**
 * 企业实名
 * @author liushaomin
 */
public class Real_nameModel extends Model<Real_nameModel>{
	
	private static final long serialVersionUID = -8257786132494558838L;
	public static final Real_nameModel dao = new Real_nameModel();
	
	/**
	 * 得到认证信息 Json
	 * @author liushaomin
	 * 2017年5月4日
	 * @param phone
	 * @return
	 */
	public static Real_nameModel getRealName_json(String phone) {
		String sql = "select buyer_phone, legal_person, company_name, org_code, region, address, idcard_just_img, idcard_back_img, license_img, state from real_name where buyer_phone = '"+ phone +"'";
		Real_nameModel realname = dao.findFirst(sql);
		return realname;
	}
	
	/**
	 * 
	 * @author liushaomin
	 * 2017年5月5日
	 * @param phone
	 * @return
	 */
	public static Real_nameModel getRealName(String phone) {
		String sql = "select * from real_name where buyer_phone = '"+ phone +"'";
		Real_nameModel realname = dao.findFirst(sql);
		return realname;
	}
	
	/**
	 * 后台 获取全部列表
	 * @author liushaomin
	 * 2017年5月5日
	 * @return
	 */
	public static List<Real_nameModel> getRealNameList() {
		String sql = "select * from real_name order by ctime desc";
		List<Real_nameModel> list = dao.find(sql);
		for(Real_nameModel realname : list){
			String userid = realname.get("buyer_id").toString();
			BuyerStatisticsModel shop = BuyerStatisticsModel.getStatisticsByID(userid);
			realname.put("shop_name", shop.get("shop_name"));
			UserAppModel user = UserAppModel.getById(userid);
			realname.put("user_nick", user.get("nick"));
		}
		return list;
	}

	/**
	 * 更新审核状态
	 * @author liushaomin
	 * 2017年5月5日
	 * @param id
	 * @param state
	 * @return
	 */
	public static boolean update_state(String id, String state) {
		Real_nameModel realname = dao.findById(id);
		return realname.set("state", state).update();
	}

	/**
	 * 获取企业实名认证 及其关联信息
	 * 后台用
	 * @author liushaomin
	 * 2017年5月5日
	 * @param id
	 * @return
	 */
	public static Real_nameModel getRealNameBackstage(String id) {
		Real_nameModel realname = dao.findById(id);
		String userid = realname.get("buyer_id").toString();
		BuyerStatisticsModel shop = BuyerStatisticsModel.getStatisticsByID(userid);
		realname.put("shop_name", shop.get("shop_name"));
		realname.put("shop_saletype", GoodsCategroyModel.getShop_saletypeName(shop.get("shop_saletype").toString()));
		UserAppModel user = UserAppModel.getById(userid);
		realname.put("buyer_nick", user.get("nick"));
		return realname;
	}
}
